package com.czf.personal.controller;

import com.czf.common.entity.UserEconomicCapacity;
import com.czf.common.util.AjaxResult;
import com.czf.personal.service.UserEconomicCapacityServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zfChen
 * @create 2022/4/20 15:22
 */
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/economic")
public class EconomicCapacityController {

    @Autowired
    private UserEconomicCapacityServiceImpl userEconomicCapacityService;

    @RequestMapping(value = "hold", method = RequestMethod.POST)
    public AjaxResult insert(@RequestBody UserEconomicCapacity userEconomicCapacity) {
        UserEconomicCapacity select = userEconomicCapacityService.select(userEconomicCapacity.getUserId());
        if (ObjectUtils.isNotEmpty(select)) {
            int update = userEconomicCapacityService.update(userEconomicCapacity);
            if (update == 1) {
                return AjaxResult.success("更新成功");
            }
        } else {
            int insert = userEconomicCapacityService.insert(userEconomicCapacity);
            if (insert == 1) {
                return AjaxResult.success("插入成功");
            }
        }
        return AjaxResult.error();
    }

    @RequestMapping(value = "select", method = RequestMethod.GET)
    public AjaxResult select(Long userId) {
        UserEconomicCapacity select = userEconomicCapacityService.select(userId);
        if (ObjectUtils.isNotEmpty(select)) {
            return AjaxResult.success(select);
        }
        return AjaxResult.error();
    }
}
